package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domian.Category;
import com.itheima.reggie.domian.Dish;
import com.itheima.reggie.domian.DishFlavor;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    //分页查询
    @GetMapping("/page")
    public R<Page<DishDto>> page(String name, Integer page, Integer pageSize) {
        Page<Dish> pageInfo1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.like(StringUtils.isNotBlank(name), Dish::getName, name);
        dishService.page(pageInfo1, queryWrap);
        //将数据复制到pageInfo2中
        Page<DishDto> pageInfo2 = new Page<>(page, pageSize);
        //将pageInfo1除了records的其他数据复制到pageInfo2中
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");
        //getRecords得到pageInfo1中的list<Dish>集合.map做映射,将集合对象返回一个新的对象
        List<DishDto> dtoList = pageInfo1.getRecords().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            //将dish数据复制到disDto里面
            BeanUtils.copyProperties(dish, dishDto);
            //dish外键==菜品分类主键,得到菜品分类名称
            Long id = dish.getCategoryId();
            LambdaQueryWrapper<Category> wrap = new LambdaQueryWrapper<>();
            wrap.eq(Category::getId, id);
            Category category = categoryService.getOne(wrap);
            //将查询到的分类名赋值给dishDto
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        pageInfo2.setRecords(dtoList);
        return R.success(pageInfo2);
    }

    //新增菜品 已更新
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.save1(dishDto);
        //如果有操作数据库的动作,清空对应菜品的缓存
        String key ="dishList_"+ dishDto.getCategoryId();
        redisTemplate.delete(key);
        return R.success("添加成功");
    }

    //根据id查询 回显
    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    //编辑保存 已更新
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        //清空所有菜品缓存
        Set keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    //根据分类查询菜品 已更新
    @RequestMapping("/list")
    public R<List<DishDto>> selectList(Dish dish) {
        //当用户访问的时候先不去访问mysql数据库,先去访问redis数据库
        String redis = "dishList_"+dish.getCategoryId();
        List<DishDto> key = (List<DishDto>) redisTemplate.opsForValue().get(redis);
        if (key!=null){
            return R.success(key);
        }
        LambdaQueryWrapper<Dish> wrap = new LambdaQueryWrapper<>();
        wrap.eq(dish.getCategoryId()!=null,Dish::getCategoryId, dish.getCategoryId());
        List<Dish> list = dishService.list(wrap);
        List<DishDto> dishDtoList = list.stream().map(item -> {
            DishDto dishDto = new DishDto();
            //基本属性拷贝
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            LambdaQueryWrapper<DishFlavor> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        //将数据保存到redis缓存
        redisTemplate.opsForValue().set(redis,dishDtoList);
        return R.success(dishDtoList);
    }
}
